#include <iostream>
#include <string>
#include <vector>

using namespace std;

// Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode* next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        vector<ListNode*> list;
        while (head != nullptr) {
            list.push_back(head);
            head = head->next;
        }
        ListNode tmp_node = ListNode();
        ListNode* tmp_p = &tmp_node;
        int length = list.size();
        for (int i = length -1; i>=0; i--) {
            tmp_p->next = list[i];
            tmp_p = tmp_p->next;
        }
        tmp_p->next = nullptr;
        return tmp_node.next;

    }
};